﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class PickingRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15431;
        protected GridView gvPickingByOrderRpt;
        protected GridView gvPickingRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalGrossWgt;
        protected Label lblTotalGrossWgtText;
        protected Label lblTotalNetWgt;
        protected Label lblTotalNetWgtText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalCube;
        private decimal TotalGrossWgt;
        private decimal TotalNetWgt;
        private decimal TotalQty;
        protected UpdatePanel up1;

        private void BindData2gvPickingRptList(PickingRptQueryEntity PickingRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(PickingRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetPickingRptInfo(PickingRptQuery);
            if (PickingRptQuery.IS_GROUP == "Y")
            {
                this.gvPickingByOrderRpt.Visible = true;
                this.gvPickingRpt.Visible = false;
                base.BindData2GridView(this.gvPickingByOrderRpt, ds);
            }
            else
            {
                this.gvPickingRpt.Visible = true;
                this.gvPickingByOrderRpt.Visible = false;
                base.BindData2GridView(this.gvPickingRpt, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            pickingRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = pickingRptQuery.CurrentPage;
            pickingRptQuery.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetPickingRptInfo(pickingRptQuery))
                {
                    if (pickingRptQuery.IS_GROUP == "Y")
                    {
                        base.Export2Excel("PickingRpt.xls", "15431", this.gvPickingByOrderRpt, ds);
                    }
                    else
                    {
                        base.Export2Excel("PickingRpt.xls", "15431", this.gvPickingRpt, ds);
                    }
                }
            }
            finally
            {
                pickingRptQuery.IsGetAll = false;
                pickingRptQuery.CurrentPage = currentPageIndex;
                this.SetPickingRptQueryEntity(pickingRptQuery);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            pickingRptQuery.CurrentPage = "0";
            this.BindData2gvPickingRptList(pickingRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            pickingRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvPickingRptList(pickingRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            pickingRptQuery.ORDER_ID = "";
            pickingRptQuery.EXTERN_ORDER_ID = "";
            pickingRptQuery.REFERENCE_NUM = "";
            pickingRptQuery.OWNER_ID = "";
            pickingRptQuery.ORDER_TYPE = "";
            pickingRptQuery.StartDateTime = "";
            pickingRptQuery.EndDateTime = "";
            pickingRptQuery.SKU_CLASS_ID = "";
            pickingRptQuery.SKU_ID = "";
            pickingRptQuery.SKU_DESCR = "";
            pickingRptQuery.CUSTOMER_ID = "";
            pickingRptQuery.IS_GROUP = "";
            pickingRptQuery.LOT_ATTR01 = "";
            pickingRptQuery.LOT_ATTR02 = "";
            pickingRptQuery.LOT_ATTR03 = "";
            pickingRptQuery.LOT_ATTR04 = "";
            pickingRptQuery.LOT_ATTR05 = "";
            pickingRptQuery.LOT_ATTR06 = "";
            pickingRptQuery.LOT_ATTR07 = "";
            pickingRptQuery.LOT_ATTR08 = "";
            pickingRptQuery.LOT_ATTR09 = "";
            pickingRptQuery.LOT_ATTR10 = "";
            pickingRptQuery.PRODUCE_DATE = "";
            pickingRptQuery.EXTERNAL_LOT = "";
            pickingRptQuery.EXPIRY_DATE = "";
            pickingRptQuery.LPN_ID = "";
            pickingRptQuery.VENDOR_ID = "";
            this.BindData2gvPickingRptList(pickingRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            if (int.Parse(pickingRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                pickingRptQuery.CurrentPage = (int.Parse(pickingRptQuery.CurrentPage) + 1).ToString();
                this.BindData2gvPickingRptList(pickingRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            if (int.Parse(pickingRptQuery.CurrentPage) > 0)
            {
                pickingRptQuery.CurrentPage = (int.Parse(pickingRptQuery.CurrentPage) - 1).ToString();
                this.BindData2gvPickingRptList(pickingRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
            this.BindData2gvPickingRptList(pickingRptQuery);
        }

        private PickingRptQueryEntity GetPickingRptQueryEntity()
        {
            return (base.PageQueryEntitySession as PickingRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
                pickingRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvPickingRptList(pickingRptQuery);
            }
        }

        protected void gvPickingByOrderRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15431", this.gvPickingByOrderRpt);
        }

        protected void gvPickingByOrderRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
                }
                if (((Label) e.Row.FindControl("lblDiffQty")) != null)
                {
                    Label lblDiffQty = (Label) e.Row.FindControl("lblDiffQty");
                    if (!(string.IsNullOrEmpty(lblDiffQty.Text) || !(decimal.Parse(lblDiffQty.Text) != 0M)))
                    {
                        lblDiffQty.BackColor = Color.Yellow;
                    }
                }
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQty")).Text != "") ? ((Label) e.Row.FindControl("lblQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalGrossWgt")) != null)
                {
                    this.TotalGrossWgt += decimal.Parse((((Label) e.Row.FindControl("lblTotalGrossWgt")).Text != "") ? ((Label) e.Row.FindControl("lblTotalGrossWgt")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalNetWgt")) != null)
                {
                    this.TotalNetWgt += decimal.Parse((((Label) e.Row.FindControl("lblTotalNetWgt")).Text != "") ? ((Label) e.Row.FindControl("lblTotalNetWgt")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalCube")) != null)
                {
                    this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblTotalCube")).Text != "") ? ((Label) e.Row.FindControl("lblTotalCube")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalGrossWgtText.Text = base.GetPreferenceDigit(this.TotalGrossWgt);
                this.lblTotalNetWgtText.Text = base.GetPreferenceDigit(this.TotalNetWgt);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
            }
        }

        protected void gvPickingByOrderRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
                pickingRptQuery.SortField = e.SortExpression;
                if (pickingRptQuery.SortDirection == "ASC")
                {
                    pickingRptQuery.SortDirection = "DESC";
                }
                else if (pickingRptQuery.SortDirection == "DESC")
                {
                    pickingRptQuery.SortDirection = "ASC";
                }
                this.BindData2gvPickingRptList(pickingRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvPickingRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15431", this.gvPickingRpt);
        }

        protected void gvPickingRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
                }
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQty")).Text != "") ? ((Label) e.Row.FindControl("lblQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalGrossWgt")) != null)
                {
                    this.TotalGrossWgt += decimal.Parse((((Label) e.Row.FindControl("lblTotalGrossWgt")).Text != "") ? ((Label) e.Row.FindControl("lblTotalGrossWgt")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalNetWgt")) != null)
                {
                    this.TotalNetWgt += decimal.Parse((((Label) e.Row.FindControl("lblTotalNetWgt")).Text != "") ? ((Label) e.Row.FindControl("lblTotalNetWgt")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalCube")) != null)
                {
                    this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblTotalCube")).Text != "") ? ((Label) e.Row.FindControl("lblTotalCube")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalGrossWgtText.Text = base.GetPreferenceDigit(this.TotalGrossWgt);
                this.lblTotalNetWgtText.Text = base.GetPreferenceDigit(this.TotalNetWgt);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
            }
        }

        protected void gvPickingRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PickingRptQueryEntity pickingRptQuery = this.GetPickingRptQueryEntity();
                pickingRptQuery.SortField = e.SortExpression;
                if (pickingRptQuery.SortDirection == "ASC")
                {
                    pickingRptQuery.SortDirection = "DESC";
                }
                else if (pickingRptQuery.SortDirection == "DESC")
                {
                    pickingRptQuery.SortDirection = "ASC";
                }
                this.BindData2gvPickingRptList(pickingRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PickingRptQueryEntity pickingRptQuery = new PickingRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    pickingRptQuery.ORDER_ID = this.PreviousPage.OrderID;
                    pickingRptQuery.EXTERN_ORDER_ID = this.PreviousPage.ExternOrderID;
                    pickingRptQuery.REFERENCE_NUM = this.PreviousPage.ReferenceNum;
                    pickingRptQuery.WaveID = this.PreviousPage.WaveID;
                    pickingRptQuery.Status = this.PreviousPage.Status;
                    pickingRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    pickingRptQuery.ORDER_TYPE = this.PreviousPage.OrderType;
                    pickingRptQuery.StartDateTime = this.PreviousPage.StartDateTime;
                    pickingRptQuery.EndDateTime = this.PreviousPage.EndDateTime;
                    pickingRptQuery.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    pickingRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    pickingRptQuery.SKU_DESCR = this.PreviousPage.SkuDescr;
                    pickingRptQuery.CUSTOMER_ID = this.PreviousPage.CustomerID;
                    pickingRptQuery.IS_GROUP = this.PreviousPage.IsGroup;
                    pickingRptQuery.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    pickingRptQuery.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    pickingRptQuery.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    pickingRptQuery.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    pickingRptQuery.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    pickingRptQuery.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    pickingRptQuery.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    pickingRptQuery.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    pickingRptQuery.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    pickingRptQuery.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    pickingRptQuery.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                    pickingRptQuery.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                    pickingRptQuery.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    pickingRptQuery.LPN_ID = this.PreviousPage.LpnID;
                    pickingRptQuery.VENDOR_ID = this.PreviousPage.VendorID;
                }
                else
                {
                    pickingRptQuery.ORDER_ID = "";
                    pickingRptQuery.EXTERN_ORDER_ID = "";
                    pickingRptQuery.REFERENCE_NUM = "";
                    pickingRptQuery.WaveID = "";
                    pickingRptQuery.Status = "";
                    pickingRptQuery.OWNER_ID = "";
                    pickingRptQuery.ORDER_TYPE = "";
                    pickingRptQuery.StartDateTime = "";
                    pickingRptQuery.EndDateTime = "";
                    pickingRptQuery.SKU_CLASS_ID = "";
                    pickingRptQuery.SKU_ID = "";
                    pickingRptQuery.SKU_DESCR = "";
                    pickingRptQuery.CUSTOMER_ID = "";
                    pickingRptQuery.IS_GROUP = "";
                    pickingRptQuery.LOT_ATTR01 = "";
                    pickingRptQuery.LOT_ATTR02 = "";
                    pickingRptQuery.LOT_ATTR03 = "";
                    pickingRptQuery.LOT_ATTR04 = "";
                    pickingRptQuery.LOT_ATTR05 = "";
                    pickingRptQuery.LOT_ATTR06 = "";
                    pickingRptQuery.LOT_ATTR07 = "";
                    pickingRptQuery.LOT_ATTR08 = "";
                    pickingRptQuery.LOT_ATTR09 = "";
                    pickingRptQuery.LOT_ATTR10 = "";
                    pickingRptQuery.PRODUCE_DATE = "";
                    pickingRptQuery.EXTERNAL_LOT = "";
                    pickingRptQuery.EXPIRY_DATE = "";
                    pickingRptQuery.LPN_ID = "";
                    pickingRptQuery.VENDOR_ID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            pickingRptQuery.IsGetAll = false;
            pickingRptQuery.CurrentPage = "0";
            pickingRptQuery.PageSize = this.gvPickingRpt.PageSize.ToString();
            pickingRptQuery.SortField = "ORDER_ID";
            pickingRptQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvPickingRpt.PageSize;
            this.SetPickingRptQueryEntity(pickingRptQuery);
            this.BindData2gvPickingRptList(pickingRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvPickingRpt);
                base.CreateInitGridviewSQL("15431", this.gvPickingRpt);
                this.LoadData();
            }
        }

        private void SetPickingRptQueryEntity(PickingRptQueryEntity PickingRptQuery)
        {
            base.PageQueryEntitySession = PickingRptQuery;
        }

        public PickingRptQuery PreviousPage
        {
            get
            {
                return (PickingRptQuery) base.PreviousPage;
            }
        }
    }
}

